package ccy.ccykills.listener;

import ccy.ccykills.config.CcyConfig;
import ccy.ccykills.config.KillData;
import ccy.ccykills.event.KillPlayerEvent;
import ccy.ccykills.playerdata.PlayerData;
import ccy.ccylibrary.plugin.annotation.Event;
import org.bukkit.Bukkit;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.projectiles.ProjectileSource;

@Event
public class EntityDamageByEntityListener implements Listener {
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (!CcyConfig.config.playerNumMap.containsKey(player.getUniqueId())) {
            CcyConfig.config.playerNumMap.put(player.getUniqueId(), new KillData((new PlayerData(player)).getNum(), false));
        }

    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        if (CcyConfig.config.playerNumMap.containsKey(player.getUniqueId())) {
            KillData killData = (KillData)CcyConfig.config.playerNumMap.get(player.getUniqueId());
            if (killData.isB()) {
                (new PlayerData(player)).setNum(killData.getValue());
            }

            CcyConfig.config.playerNumMap.remove(event.getPlayer().getUniqueId());
        }

    }

    @EventHandler(
            ignoreCancelled = true
    )
    public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
        Entity damager = event.getDamager();
        if (damager instanceof Arrow) {
            Arrow arrow = (Arrow)damager;
            ProjectileSource shooter = arrow.getShooter();
            if (!(shooter instanceof Player)) {
                return;
            }

            damager = (Entity)shooter;
        } else if (!(damager instanceof Player)) {
            return;
        }

        Entity entity = event.getEntity();
        if (entity instanceof Player) {
            Player targetPlayer = (Player)entity;
            Player damagerPlayer = (Player)damager;
            if (targetPlayer.getHealth() - event.getFinalDamage() <= (double)0.0F) {
                KillPlayerEvent killPlayerEvent = new KillPlayerEvent(targetPlayer);
                Bukkit.getPluginManager().callEvent(killPlayerEvent);
                if (killPlayerEvent.isCancelled()) {
                    return;
                }

                if (CcyConfig.config.playerNumMap.containsKey(damagerPlayer.getUniqueId())) {
                    KillData killData = CcyConfig.config.playerNumMap.get(damagerPlayer.getUniqueId());
                    killData.setB(true);
                    killData.setValue(killData.getValue() + 1);
                } else {
                    CcyConfig.config.playerNumMap.put(damagerPlayer.getUniqueId(), new KillData((new PlayerData(damagerPlayer)).getNum() + 1, false));
                }
            }
        }

    }
}
